package com.woniuxy.service.impl;

import com.woniuxy.entity.ShopOrderItems;
import com.woniuxy.dao.ShopOrderItemsDao;
import com.woniuxy.entity.Storage;
import com.woniuxy.feignclient.StorageFeignClient;
import com.woniuxy.service.ShopOrderItemsService;
import io.seata.spring.annotation.GlobalTransactional;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * (ShopOrderItems)表服务实现类
 *
 * @author makejava
 * @since 2025-07-10 11:54:15
 */
@Service("shopOrderItemsService")
public class ShopOrderItemsServiceImpl implements ShopOrderItemsService {
    @Autowired
    private ShopOrderItemsDao shopOrderItemsDao;
    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    StorageFeignClient storageFeignClient;
    @GlobalTransactional
    @Override
    public List<ShopOrderItems> insert2(List<ShopOrderItems> itemsList) {
        RLock lock = redissonClient.getLock("myLock");
        try {
            // 加锁（最长等待30秒，自动释放锁时间10秒）
            lock.tryLock(30, 10, TimeUnit.SECONDS);
            // 业务逻辑
                // 创建一个List<Storage>集合来存储所有Storage对象
                List<Storage> storageList = new ArrayList<>();
                // 遍历列表，逐个插入（或使用更高效的批量插入）
                for (ShopOrderItems item : itemsList) {
                    shopOrderItemsDao.insert(item);
                    // 创建Storage对象并添加到集合中
                    Storage storage = new Storage(null, item.getProductId(), item.getQuantity());
                    storageList.add(storage);
                }
                // 远程调用扣减库存
                storageFeignClient.deduct(storageList);
//                return itemsList;
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } finally {
            // 释放锁
            if (lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }
        return itemsList;
    }


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ShopOrderItems queryById(Long id) {
        return this.shopOrderItemsDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param shopOrderItems 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<ShopOrderItems> queryByPage(ShopOrderItems shopOrderItems, PageRequest pageRequest) {
        long total = this.shopOrderItemsDao.count(shopOrderItems);
        return new PageImpl<>(this.shopOrderItemsDao.queryAllByLimit(shopOrderItems, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param
     * @return 实例对象
     */

    @GlobalTransactional
    @Override
    public List<ShopOrderItems> insert(List<ShopOrderItems> itemsList) {
        // 创建一个List<Storage>集合来存储所有Storage对象
        List<Storage> storageList = new ArrayList<>();
        // 遍历列表，逐个插入（或使用更高效的批量插入）
        for (ShopOrderItems item : itemsList) {
            shopOrderItemsDao.insert(item);
            // 创建Storage对象并添加到集合中
            Storage storage = new Storage(null, item.getProductId(), item.getQuantity());
            storageList.add(storage);
        }
        // 远程调用扣减库存
        storageFeignClient.deduct(storageList);
        return itemsList;
    }

    /**
     * 修改数据
     *
     * @param shopOrderItems 实例对象
     * @return 实例对象
     */
    @Override
    public ShopOrderItems update(ShopOrderItems shopOrderItems) {
        this.shopOrderItemsDao.update(shopOrderItems);
        return this.queryById(shopOrderItems.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.shopOrderItemsDao.deleteById(id) > 0;
    }
}
